/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.model.DistributionUserWallet;
import com.yami.shop.distribution.comment.model.DistributionWithdrawCash;
import com.yami.shop.distribution.comment.param.RangeTimeParam;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.distribution.comment.service.DistributionUserWalletService;
import com.yami.shop.distribution.comment.service.DistributionWithdrawCashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/platform/distributionWithdrawCash")
public class DistributionWithdrawCashController {

    @Autowired
    private DistributionWithdrawCashService distributionWithdrawCashService;
    @Autowired
    private DistributionUserWalletService distributionUserWalletService;
    @Autowired
    private DistributionUserService distributionUserService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('platform:distributionWithdrawCash:page')")
    public ResponseEntity<IPage<DistributionWithdrawCash>> page(PageParam<DistributionWithdrawCash> page, RangeTimeParam rangeTimeParam,
                                                                String userMobile, DistributionWithdrawCash distributionWithdrawCash) {
        IPage<DistributionWithdrawCash> list = distributionWithdrawCashService.distributionWithdrawCashsPage(page, null, rangeTimeParam, userMobile, distributionWithdrawCash);
        return ResponseEntity.ok(list);
    }

    /**
     * 修改
     */
    @PutMapping("/toSuccess/{withdrawCashId}")
    @PreAuthorize("@pms.hasPermission('platform:distributionWithdrawCash:update')")
    public ResponseEntity<Void> update(@PathVariable String withdrawCashId) {
        DistributionWithdrawCash distributionWithdrawCash = distributionWithdrawCashService.getById(withdrawCashId);
        if (distributionWithdrawCash == null) {
            throw new YamiShopBindException("yami.get.user.cash.info");
        }
        Long walletId = distributionWithdrawCash.getWalletId();

        DistributionUserWallet wallet = distributionUserWalletService.getById(walletId);

        if (wallet == null) {
            throw new YamiShopBindException("yami.get.user.withdraw.info");
        }

        DistributionUser distributionUser = distributionUserService.getById(wallet.getDistributionUserId());

        if (distributionUser == null) {
            throw new YamiShopBindException("yami.user.no.exist");
        }

        if (!Objects.equals(distributionUser.getShopId(), Constant.PLATFORM_SHOP_ID)) {
            throw new YamiShopBindException("yami.withdrawCash.update.no.auth");
        }

        distributionWithdrawCash.setState(1);
        distributionWithdrawCash.setUpdateTime(new Date());
        distributionWithdrawCashService.updateById(distributionWithdrawCash);
        return ResponseEntity.ok().build();
    }

}
